# Import required modules
import csv
import sqlite3

# Connecting to the geeks database
connection = sqlite3.connect('../../db.sqlite3')

# Creating a cursor object to execute
# SQL queries on a database table
cursor = connection.cursor()

# Table Definition
create_table = '''
create table data_Chassis
(
    Name text not null,
    PID text not null,
    Platform text not null,
    Price text,
    Capacity text,
    Color text,
    Slot text,
    TranType text,
    constraint chassis_pk
        primary key (Name,PID,Platform)
);


'''

# Creating the table into our
# database
cursor.execute(create_table)

# Opening the person-records.csv file
file = open('../Details/ChassisInfo.csv',encoding='utf-8')

# Reading the contents of the
# person-records.csv file
contents = csv.reader(file)

# SQL query to insert data into the
# person table
insert_records = "INSERT INTO data_Chassis (Name,PID,Platform,Price,Capacity,Color,Slot,TranType) VALUES(?, ?,?, ?,?, ?,?, ?)"

# Importing the contents of the file
# into our person table
cursor.executemany(insert_records, contents)

# SQL query to retrieve all data from
# the person table To verify that the
# data of the csv file has been successfully
# inserted into the table
select_all = "SELECT * FROM data_Chassis"
rows = cursor.execute(select_all).fetchall()

# Output to the console screen
for r in rows:
    print(r)

# Committing the changes
connection.commit()

# closing the database connection
connection.close()
